package com.english_test_sys.controller;

import com.english_test_sys.pojo.Question;
import com.english_test_sys.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/questions")
public class QuestionController {

    @Autowired
    private QuestionService questionService;

    // 获取指定分类和类型的问题列表
    @GetMapping
    public ResponseEntity<List<Question>> getQuestionsByCategoryAndType(
            @RequestParam(value = "category", required = false) String category,
            @RequestParam(value = "type", required = false) String type) {
        if (category != null && type != null) {
            return ResponseEntity.ok(questionService.getQuestionsByCategoryAndType(category, type));
        }else {
            return ResponseEntity.badRequest().build();
        }
    }


    @GetMapping("/difficulty")
    public ResponseEntity<List<Question>> getQuestionsByDifficulty(
            @RequestParam(value = "difficulty", required = false) Integer difficulty) {
        if (difficulty != null) {
            return ResponseEntity.ok(questionService.getQuestionsByDifficulty(difficulty));
        }else {
            return ResponseEntity.badRequest().build();
        }
    }
    // 添加新问题
    @PostMapping
    public ResponseEntity<Question> importQuestion(@RequestBody Question question) {
        questionService.importQuestion(question);
        return ResponseEntity.ok(question);
    }

    // 根据ID获取题目
    @GetMapping("/{id}")
    public ResponseEntity<Question> getQuestionById(@PathVariable Integer id) {
        Question question = questionService.getQuestionById(id); // 需要Service层实现
        if (question == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(question); // 200 返回题目
    }

    // 更新问题
    @PutMapping("/{id}")
    public ResponseEntity<Question> updateQuestion(@PathVariable Integer id, @RequestBody Question question) {
        question.setId(id);
        questionService.updateQuestion(question);
        return ResponseEntity.ok(question);
    }

    // 删除问题
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteQuestion(@PathVariable Integer id) {
        questionService.deleteQuestion(id);
        return ResponseEntity.noContent().build();
    }

    @GetMapping("/wrong_questions")
    public String wrong_questions() {
        return "wrong_questions";
    }
}